using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace UKVSTS.FieldDefaultValues
{
    public class FieldDefaultValueConfig : AddinConfig
    {

//        List<ProjectFieldDefaultValues> m_ProjectDefaults = new List<ProjectFieldDefaultValues>();

        private const String CONFIG_FILENAME = "FieldDefaultValueConfig.dat";

        private static FieldDefaultValueConfig ms_Instance = new FieldDefaultValueConfig();

        private FieldDefaultValueConfig()
            : base(CONFIG_FILENAME) 
        {
            if (Load() == false || ProjectDefaults == null)
            {
                Properties["ProjectFieldDefaultValues"] = new List<ProjectFieldDefaultValues>();
            }
        }

        private List<ProjectFieldDefaultValues> ProjectDefaults
        {
            get { return Properties["ProjectFieldDefaultValues"] as List<ProjectFieldDefaultValues>; }
        }

        public static FieldDefaultValueConfig Instance
        {
            get { return ms_Instance; }
        }

        public List<FieldDefaultValue> GetDefaultsForWorkItem(WorkItemType wit)
        {
            return GetDefaultsForWorkItem(wit.Project.Name, wit.Name);
        }

        public List<FieldDefaultValue> GetDefaultsForWorkItem(String projectName, String wiName)
        {
            List<FieldDefaultValue> values = null;
            ProjectFieldDefaultValues projectValues = FindValuesForWorkItem(projectName, wiName);

            if (projectValues == null)
            {
                values = new List<FieldDefaultValue>();
            }
            else
            {
                // Create a deep copy of this collection and return it.
                //
                values = new List<FieldDefaultValue>(projectValues.DefaultValues.Count);
                foreach (FieldDefaultValue fdv in projectValues.DefaultValues)
                {
                    values.Add(new FieldDefaultValue(fdv.FieldName, fdv.DefaultValue));
                }
            }

            return values;
        }

        private ProjectFieldDefaultValues FindValuesForWorkItem (String projectName, String wiName)
        {
            return ProjectDefaults.Find(delegate(ProjectFieldDefaultValues pv)
                                                    { return pv.ProjectName.Equals(projectName) &&
                                                             pv.WorkItemTypeName.Equals (wiName); });
        }
        
        public void SetDefaultsForWorkItem(WorkItemType wit, List<FieldDefaultValue> values)
        {
            SetDefaultsForWorkItem(wit.Project.Name, wit.Name, values);
        }

        public void SetDefaultsForWorkItem(String projectName, String wiName, List<FieldDefaultValue> values)
        {
            ProjectFieldDefaultValues projectValues = FindValuesForWorkItem(projectName, wiName);

            if (projectValues == null)
            {
                projectValues = new ProjectFieldDefaultValues(projectName, wiName);
                ProjectDefaults.Add(projectValues);
            }
            projectValues.DefaultValues = values;

            Save();
        }


    }
}
